package com.geofitti.andoird;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; 
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.*;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.apache.http.client.*;
import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy;



public final class WebCall {
	private static String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the BufferedReader.readLine()
         * method. We iterate until the BufferedReader return null which means
         * there's no more data to read. Each line will appended to a StringBuilder
         * and returned as String.
         */
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
	
    public static JSONObject doGet(String url) throws JSONException
    {
    	ThreadPolicy tp = ThreadPolicy.LAX;
    	StrictMode.setThreadPolicy(tp);
    	
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url); 
        HttpResponse response;
        try {
            response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
               // InputStream instream = entity.getContent(); 
               // JSONArray array = new JSONArray(EntityUtils.toString(entity));
                //JSONObject result = array.getJSONObject(0);
                //instream.close();
            	JSONObject result = new JSONObject(EntityUtils.toString(entity));
                return result;
            }
        } catch (ClientProtocolException e) {
        	return new JSONObject().put("just", "clientexception");
        } catch (IOException e) {
        	e.printStackTrace();
        	return new JSONObject().put("just", "ioexception");
        }
        return null;
    }
    
    public static String doPost(String url, ArrayList<NameValuePair> params){
    	ThreadPolicy tp = ThreadPolicy.LAX;
    	StrictMode.setThreadPolicy(tp);
        
        try {
        	HttpClient httpclient = new DefaultHttpClient();
            HttpPost httpget = new HttpPost(url); 
            if (params != null){
	            httpget.setEntity(new UrlEncodedFormEntity(params));
            }
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                String result= convertStreamToString(instream);
                instream.close();
                return result;
            }
        } catch (ClientProtocolException e) {
        	return e.toString();
        } catch (IOException e) {
        	return e.toString();
        }
        return null;
    }
}
